package com.andrognito.patternlockview.utils;

import com.andrognito.patternlockview.PatternLockView;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes.dex */
public class PatternLockUtils {
    private static final String MD5 = "MD5";
    private static final String SHA1 = "SHA-1";
    private static final String UTF8 = "UTF-8";

    private PatternLockUtils() {
        throw new AssertionError("You can not instantiate this class. Use its static utility methods instead");
    }

    public static String patternToString(PatternLockView patternLockView, List<PatternLockView.Dot> list) {
        if (list == null) {
            return "";
        }
        int size = list.size();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            PatternLockView.Dot dot = list.get(i);
            sb.append((dot.getRow() * patternLockView.getDotCount()) + dot.getColumn());
        }
        return sb.toString();
    }

    public static List<PatternLockView.Dot> stringToPattern(PatternLockView patternLockView, String str) {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < str.length(); i++) {
            int numericValue = Character.getNumericValue(str.charAt(i));
            arrayList.add(PatternLockView.Dot.of(numericValue / patternLockView.getDotCount(), numericValue % patternLockView.getDotCount()));
        }
        return arrayList;
    }

    public static String patternToSha1(PatternLockView patternLockView, List<PatternLockView.Dot> list) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
            messageDigest.update(patternToString(patternLockView, list).getBytes("UTF-8"));
            byte[] digest = messageDigest.digest();
            BigInteger bigInteger = new BigInteger(1, digest);
            return String.format(null, "%0" + (digest.length * 2) + "x", bigInteger).toLowerCase();
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException unused) {
            return null;
        }
    }

    public static String patternToMD5(PatternLockView patternLockView, List<PatternLockView.Dot> list) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(MD5);
            messageDigest.update(patternToString(patternLockView, list).getBytes("UTF-8"));
            byte[] digest = messageDigest.digest();
            BigInteger bigInteger = new BigInteger(1, digest);
            return String.format(null, "%0" + (digest.length * 2) + "x", bigInteger).toLowerCase();
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException unused) {
            return null;
        }
    }

    public static ArrayList<PatternLockView.Dot> generateRandomPattern(PatternLockView patternLockView, int i) throws IndexOutOfBoundsException {
        int i2;
        int i3 = i;
        if (patternLockView == null) {
            throw new IllegalArgumentException("PatternLockView can not be null.");
        }
        if (i3 <= 0 || i3 > patternLockView.getDotCount()) {
            throw new IndexOutOfBoundsException("Size must be in range [1, " + patternLockView.getDotCount() + "]");
        }
        ArrayList<Integer> arrayList = new ArrayList();
        int randInt = RandomUtils.randInt(patternLockView.getDotCount());
        arrayList.add(Integer.valueOf(randInt));
        while (arrayList.size() < i3) {
            int dotCount = randInt / patternLockView.getDotCount();
            int dotCount2 = randInt % patternLockView.getDotCount();
            int max = Math.max(Math.max(dotCount, patternLockView.getDotCount() - dotCount), Math.max(dotCount2, patternLockView.getDotCount() - dotCount2));
            int i4 = 1;
            int i5 = 1;
            int i6 = -1;
            while (i5 <= max) {
                int i7 = dotCount - i5;
                int i8 = dotCount2 - i5;
                int i9 = dotCount + i5;
                int i10 = dotCount2 + i5;
                int[] randIntArray = RandomUtils.randIntArray(4);
                int length = randIntArray.length;
                int i11 = 0;
                while (true) {
                    if (i11 >= length) {
                        i2 = dotCount2;
                        break;
                    }
                    int i12 = randIntArray[i11];
                    if (i12 == 0) {
                        i2 = dotCount2;
                        if (i7 >= 0) {
                            int[] randIntArray2 = RandomUtils.randIntArray(Math.max(0, i8), Math.min(patternLockView.getDotCount(), i10 + 1));
                            int length2 = randIntArray2.length;
                            int i13 = 0;
                            while (i13 < length2) {
                                i6 = (patternLockView.getDotCount() * i7) + randIntArray2[i13];
                                if (!arrayList.contains(Integer.valueOf(i6))) {
                                    break;
                                }
                                i13++;
                                i6 = -1;
                            }
                        }
                    } else if (i12 == i4) {
                        i2 = dotCount2;
                        if (i10 < patternLockView.getDotCount()) {
                            int[] randIntArray3 = RandomUtils.randIntArray(Math.max(0, i7 + 1), Math.min(patternLockView.getDotCount(), i9 + 1));
                            int length3 = randIntArray3.length;
                            int i14 = 0;
                            while (i14 < length3) {
                                i6 = (randIntArray3[i14] * patternLockView.getDotCount()) + i10;
                                if (!arrayList.contains(Integer.valueOf(i6))) {
                                    break;
                                }
                                i14++;
                                i6 = -1;
                            }
                        }
                    } else if (i12 != 2) {
                        if (i12 == 3 && i8 >= 0) {
                            int[] randIntArray4 = RandomUtils.randIntArray(Math.max(0, i7 + 1), Math.min(patternLockView.getDotCount(), i9));
                            int length4 = randIntArray4.length;
                            int i15 = 0;
                            while (i15 < length4) {
                                i6 = (randIntArray4[i15] * patternLockView.getDotCount()) + i8;
                                i2 = dotCount2;
                                if (!arrayList.contains(Integer.valueOf(i6))) {
                                    break;
                                }
                                i15++;
                                dotCount2 = i2;
                                i6 = -1;
                            }
                        }
                        i2 = dotCount2;
                    } else {
                        i2 = dotCount2;
                        if (i9 < patternLockView.getDotCount()) {
                            int[] randIntArray5 = RandomUtils.randIntArray(Math.max(0, i8), Math.min(patternLockView.getDotCount(), i10));
                            int length5 = randIntArray5.length;
                            int i16 = 0;
                            while (i16 < length5) {
                                i6 = randIntArray5[i16] + (patternLockView.getDotCount() * i9);
                                if (!arrayList.contains(Integer.valueOf(i6))) {
                                    break;
                                }
                                i16++;
                                i6 = -1;
                            }
                        }
                    }
                    if (i6 >= 0) {
                        break;
                    }
                    i11++;
                    dotCount2 = i2;
                    i4 = 1;
                }
                if (i6 >= 0) {
                    break;
                }
                i5++;
                dotCount2 = i2;
                i4 = 1;
            }
            randInt = i6;
            arrayList.add(Integer.valueOf(randInt));
            i3 = i;
        }
        ArrayList<PatternLockView.Dot> arrayList2 = new ArrayList<>();
        for (Integer num : arrayList) {
            arrayList2.add(PatternLockView.Dot.of(num.intValue()));
        }
        return arrayList2;
    }
}
